package com.asiainfo.work.service.second.impl;

import com.asiainfo.work.dao.second.ZtTaskDao;
import com.asiainfo.work.entity.ZtTask;
import com.asiainfo.work.request.zentao.TaskRequest;
import com.asiainfo.work.service.second.ZtTaskService;
import com.asiainfo.work.utils.BaseResponse;
import com.asiainfo.work.utils.PageResponse;
import com.asiainfo.work.utils.PageResponseUtil;
import com.asiainfo.work.vo.TaskActualTimeStatistics;
import com.asiainfo.work.vo.TaskClosedActualTimeStatistics;
import com.asiainfo.work.vo.TaskClosedStatistics;
import com.asiainfo.work.vo.TaskEsTimeStatistics;
import com.asiainfo.work.vo.TaskStatistics;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: bf
 * @date: 2021/1/6-9:42
 */
@Slf4j
@Service
public class ZtTaskServiceImpl extends ServiceImpl<ZtTaskDao, ZtTask> implements ZtTaskService {

    @Autowired
    private ZtTaskDao ztTaskDao;

    /**
     * 查询用户任务数
     * @param taskRequest 查询条件
     * @return 分页返回任务数
     */
    @Override
    public BaseResponse<PageResponse<TaskStatistics>> queryTask(TaskRequest taskRequest) {
        log.info("[queryTask] 参数: taskRequest={}",  new Gson().toJson(taskRequest));
        IPage<TaskStatistics> plusPage = new Page<>(taskRequest.getPage(),
                taskRequest.getSize());
        String endTime = taskRequest.getEndTime();
        String startTime = taskRequest.getStartTime();
        List<String> accountList = taskRequest.getAccount();
        List<TaskStatistics> resultList = ztTaskDao.queryTask(startTime, endTime, accountList, plusPage);

        plusPage.setRecords(resultList);
        return BaseResponse.success(PageResponseUtil.getPageResponse(plusPage));
    }

    /**
     * 查询用户任务预计工时
     * @param taskRequest 查询条件
     * @return 分页返回任务数
     */
    @Override
    public BaseResponse<PageResponse<TaskEsTimeStatistics>> queryTaskEsTime(TaskRequest taskRequest) {
        log.info("[queryTaskEsTime] 参数: taskRequest={}",  new Gson().toJson(taskRequest));
        IPage<TaskEsTimeStatistics> plusPage = new Page<>(taskRequest.getPage(),
                taskRequest.getSize());
        String endTime = taskRequest.getEndTime();
        String startTime = taskRequest.getStartTime();
        List<String> accountList = taskRequest.getAccount();
        List<TaskEsTimeStatistics> resultList = ztTaskDao.queryTaskEsTime(startTime, endTime, accountList, plusPage);

        plusPage.setRecords(resultList);
        return BaseResponse.success(PageResponseUtil.getPageResponse(plusPage));
    }

    /**
     * 查询用户任务完成数
     * @param taskRequest 查询条件
     * @return 分页返回任务数
     */
    @Override
    public BaseResponse<PageResponse<TaskClosedStatistics>> queryTaskClosed(TaskRequest taskRequest) {
        log.info("[queryTaskClosed] 参数: taskRequest={}",  new Gson().toJson(taskRequest));
        IPage<TaskClosedStatistics> plusPage = new Page<>(taskRequest.getPage(),
                taskRequest.getSize());
        String endTime = taskRequest.getEndTime();
        String startTime = taskRequest.getStartTime();
        List<String> accountList = taskRequest.getAccount();
        List<TaskClosedStatistics> resultList = ztTaskDao.queryTaskClosed(startTime, endTime, accountList, plusPage);

        plusPage.setRecords(resultList);
        return BaseResponse.success(PageResponseUtil.getPageResponse(plusPage));
    }

    /**
     * 查询用户完成任务实际工时
     * @param taskRequest 查询条件
     * @return 分页返回任务数
     */
    @Override
    public BaseResponse<PageResponse<TaskClosedActualTimeStatistics>> queryTaskClosedActualTime(TaskRequest taskRequest) {
        log.info("[queryTaskClosedActualTime] 参数: taskRequest={}",  new Gson().toJson(taskRequest));
        IPage<TaskClosedActualTimeStatistics> plusPage = new Page<>(taskRequest.getPage(),
                taskRequest.getSize());
        String endTime = taskRequest.getEndTime();
        String startTime = taskRequest.getStartTime();
        List<String> accountList = taskRequest.getAccount();
        List<TaskClosedActualTimeStatistics> resultList = ztTaskDao.queryTaskClosedActualTime(startTime, endTime, accountList, plusPage);

        plusPage.setRecords(resultList);
        return BaseResponse.success(PageResponseUtil.getPageResponse(plusPage));
    }


    /**
     * 查询用户开发任务实际工时
     * @param taskRequest 查询条件
     * @return 分页返回任务数
     */
    @Override
    public BaseResponse<PageResponse<TaskActualTimeStatistics>> queryTaskActualTime(TaskRequest taskRequest) {
        log.info("[queryTaskActualTime] 参数: taskRequest={}",  new Gson().toJson(taskRequest));
        IPage<TaskActualTimeStatistics> plusPage = new Page<>(taskRequest.getPage(),
                taskRequest.getSize());
        String endTime = taskRequest.getEndTime();
        String startTime = taskRequest.getStartTime();
        List<String> accountList = taskRequest.getAccount();
        List<TaskActualTimeStatistics> resultList = ztTaskDao.queryTaskActualTime(startTime, endTime, accountList, plusPage);

        plusPage.setRecords(resultList);
        return BaseResponse.success(PageResponseUtil.getPageResponse(plusPage));
    }
}
